package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lych.commons.utils.Constants;
import com.lych.dubbo.api.MovementApi;
import com.lych.dubbo.api.UserInfoApi;
import com.lych.dubbo.api.VideoApi;
import com.lych.model.domain.UserInfo;
import com.lych.model.mongo.Movement;
import com.lych.model.mongo.Video;
import com.lych.model.vo.MovementsVo;
import com.lych.model.vo.PageResult;
import com.lych.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult users(Integer page, Integer pagesize) {
        IPage<UserInfo> userInfoIPage = userInfoApi.findAll(page,pagesize);
        List<UserInfo> records = userInfoIPage.getRecords();
        ////查询冻结状态
        for (UserInfo record : records) {
            String key = Constants.FREEZE_USER + record.getId();
            if (redisTemplate.hasKey(key)){
                record.setUserStatus("2");
            }
        }
        return new PageResult(page,pagesize, (int) userInfoIPage.getTotal(),records);
    }

    /**
     * 用户基本资料
     * @param userId
     * @return
     */
    public UserInfo usersUserID(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        //查询冻结状态
        String key = Constants.FREEZE_USER + userId;
        if (redisTemplate.hasKey(key)){
            userInfo.setUserStatus("2");
        }
        return userInfo;

    }

    /**
     * 视频记录翻页
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    public PageResult videos(Integer page, Integer pagesize, Long uid) {
        //根据id查询视频并分页
        List<Video> list = videoApi.findByUserId(page,pagesize,uid);
        int size = list.size();
        //非空判断
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //根据id查询用户详细信息
        UserInfo userinfo = userInfoApi.findById(uid);
        //构造对象
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : list) {
            VideoVo vo = VideoVo.init(userinfo,video);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,size,vos);
    }

    /**
     * 动态分页
     * @param page
     * @param pagesize
     * @param uid
     * @param state
     * @return
     */
    public PageResult messages(Integer page, Integer pagesize, Long uid, Integer state) {
        //根据id查询动态
        List<Movement> list = movementApi.findByUserId(page,pagesize,uid,state);
        int size = list.size();
        //非空判断
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //根据id查询用户信息
        UserInfo userinfo = userInfoApi.findById(uid);
        //构造返回对象
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : list) {
            MovementsVo vo = MovementsVo.init(userinfo,movement);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,size,vos);
    }

    /**
     * 用户冻结操作
     * @param map
     * @return
     */
    public Map usersFreeze(Map map) {
        Integer userId = (Integer) map.get("userId");
        userId.toString();
        String freezingTime = (String) map.get("freezingTime");
        int i = Integer.parseInt(freezingTime);
        //判断冻结几天
        int days = 0;
        if (i == 1) {
            days = 3;
        }
        if (i == 2) {
            days = 7;
        }
        if (i == 3) {
            days = -1;
        }

        //存入ridis
        String jsonString = JSON.toJSONString(map);
        redisTemplate.opsForValue().set(Constants.FREEZE_USER + userId, jsonString, days, TimeUnit.DAYS);

        //构建返回值
        Map retMap = new HashMap();
        retMap.put("message","冻结成功");
        return retMap;

    }

    /**
     * 用户解冻操作
     * @param map
     * @return
     */
    public Map usersUnfreeze(Map map) {
        Integer userId = (Integer) map.get("userId");
        userId.toString();
        redisTemplate.delete(Constants.FREEZE_USER + userId);
        Map retMap = new HashMap();
        map.put("message","解冻成功");
        return retMap;
    }
}
